namespace HLSSignature.Utils
{
    using System;
    using System.Diagnostics;
    using System.Drawing;
    using System.IO;
    using System.Reflection;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    
    using Extensibility;
    using Microsoft.Office.Core;
    using stdole;

    using COMIStream = System.Runtime.InteropServices.ComTypes.IStream;
    using STATSTG = System.Runtime.InteropServices.ComTypes.STATSTG;
    
    public class AxHost2 : AxHost
    {
        public AxHost2() : base(null)
        {
        }
    
        public static new IPictureDisp GetIPictureDispFromPicture(Image img)
        {
            return (IPictureDisp) AxHost.GetIPictureDispFromPicture(img);
        }

        public static new Image GetPictureFromIPicture(Object picture)
        {
            return AxHost.GetPictureFromIPicture(picture);
        }
    }


    public class COMStream : Stream, IDisposable
    {
#region Constants
    
        public enum STATFLAG
        {
            STATFLAG_DEFAULT    = 0x00000000,
            STATFLAG_NONAME     = 0x00000001,
        }
    
        public enum STGC
        {
            STGC_DEFAULT        = 0x00000000,
            STGC_OVERWRITE      = 0x00000000,
            STGC_ONLYIFCURRENT  = 0x00000000,
            STGC_DANGEROUSLYCOMMITMERELYTODISKCACHE = 0x00000000,
            STGC_CONSOLIDATE    = 0x00000000,
        }
    
        [Flags]
        public enum STGM
        {
            // Access
            STGM_READ           = 0x00000000,
            STGM_WRITE          = 0x00000001,
            STGM_READWRITE      = 0x00000002,
    
            // Sharing
            STGM_SHARE_DENY_NONE    = 0x00000040,
            STGM_SHARE_DENY_READ    = 0x00000030,
            STGM_SHARE_DENY_WRITE   = 0x00000020,
            STGM_SHARE_EXCLUSIVE    = 0x00000010,
            STGM_PRIORITY           = 0x00040000,
    
            // Creation
            STGM_CREATE         = 0x00001000,
            STGM_CONVERT        = 0x00020000,
            STGM_FAILIFTHERE    = 0x00000000,
    
            // Transactioning
            STGM_DIRECT         = 0x00000000,
            STGM_TRANSACTED     = 0x00010000,
    
            // Transactioning Performance
            STGM_NOSCRATCH      = 0x00100000,
            STGM_NOSNAPSHOT     = 0x00200000,
    
            // Direct SWMR and Simple
            STGM_SIMPLE         = 0x08000000,
            STGM_DIRECT_SWMR    = 0x00400000,
    
            // Delete On Release
            STGM_DELETEONRELEASE    = 0x04000000,
        }
    
        public enum STREAM_SEEK
        {
            STREAM_SEEK_SET     = 0x00000000,
            STREAM_SEEK_CUR     = 0x00000001,
            STREAM_SEEK_END     = 0x00000002,
        }
    
#endregion Constants
    
#region Members
    
        private COMIStream istream;
    
#endregion Members
    
#region Constructors
    
        public COMStream(object stream) : this(stream as COMIStream)
        {
        }
    
        public COMStream(COMIStream istream)
        {
            this.istream = istream;
            if (this.istream == null)
                throw new ArgumentNullException();
        }
    
#endregion Constructors
    
#region IDisposable
    
        protected override void Dispose(bool disposing)
        {
            this.istream = null;
        }
    
        private void CheckDisposed()
        {
            if (this.istream == null)
                throw new ObjectDisposedException("COMStream");
        }
    
#endregion IDisposable
    
#region Stream methods
    
        public override void Flush()
        {
            this.CheckDisposed();
    
            this.istream.Commit((int) STGC.STGC_DEFAULT);
        }
    
        public override long Seek(long offset, SeekOrigin origin)
        {
            this.CheckDisposed();
    
            STREAM_SEEK seek;
            switch (origin)
            {
                case SeekOrigin.Begin:
                    seek = STREAM_SEEK.STREAM_SEEK_SET;
                    break;
                case SeekOrigin.Current:
                    seek = STREAM_SEEK.STREAM_SEEK_CUR;
                    break;
                case SeekOrigin.End:
                    seek = STREAM_SEEK.STREAM_SEEK_END;
                    break;
    
                default:
                    throw new ArgumentException("origin");
            }
    
            long position = -1;
            unsafe
            {
                this.istream.Seek(offset, (int) seek, new IntPtr(&position));
            }
            return position;
        }
    
        public override void SetLength(long length)
        {
            this.CheckDisposed();
    
            this.istream.SetSize(length);
        }
    
        public override int Read(byte[] buffer, int offset, int count)
        {
            this.CheckDisposed();
    
            if (buffer == null)
                throw new ArgumentNullException("buffer");
            if (offset < 0)
                throw new ArgumentException("offset");
            if (count < 0)
                throw new ArgumentException("count");
    
            byte[] readbuffer = buffer;
            if (offset > 0)
                readbuffer = new byte[count];
    
            int bytesRead = 0;
            unsafe
            {
                this.istream.Read(readbuffer, count, new IntPtr(&bytesRead));
            }
    
            if (offset > 0)
                Array.Copy(readbuffer, 0, buffer, offset, bytesRead);
            return bytesRead;
        }
    
        public override void Write(byte[] buffer, int offset, int count)
        {
            this.CheckDisposed();
    
            if (buffer == null)
                throw new ArgumentNullException("buffer");
            if (offset < 0)
                throw new ArgumentException("offset");
            if (count < 0)
                throw new ArgumentException("count");
    
            byte[] writebuffer = buffer;
            if (offset > 0)
            {
                writebuffer = new byte[count];
                Array.Copy(buffer, offset, writebuffer, 0, count);
            }
    
            int bytesWritten = 0;
            unsafe
            {
                this.istream.Write(writebuffer, count, new IntPtr(&bytesWritten));
            }
            Debug.Assert(bytesWritten == count);
        }
    
#endregion Stream methods
    
#region Stream properties
    
        public override bool CanRead
        {
            get
            {
                this.CheckDisposed();
    
                // assume yes
                return true;
            }
        }
    
        public override bool CanSeek
        {
            get
            {
                this.CheckDisposed();
    
                // assume yes
                return true;
            }
        }
    
        public override bool CanWrite
        {
            get
            {
                this.CheckDisposed();
    
                // assume yes
                return true;
            }
        }
    
        public override long Length
        {
            get
            {
                this.CheckDisposed();
    
                STATSTG statstg;
                this.istream.Stat(out statstg, (int) STATFLAG.STATFLAG_NONAME);
                return statstg.cbSize;
            }
        }
    
        public override long Position
        {
            get
            {
                return this.Seek(0, SeekOrigin.Current);
            }
    
            set
            {
                long position = this.Seek(value, SeekOrigin.Begin);
                Debug.Assert(position == value);
            }
        }
    
#endregion Stream properties
    }


    [
        System.Runtime.InteropServices.InterfaceType(System.Runtime.InteropServices.ComInterfaceType.InterfaceIsIUnknown),
        System.Runtime.InteropServices.ComVisible(true),
        System.Runtime.InteropServices.Guid("00000114-0000-0000-C000-000000000046")
    ]
    public interface COMIOleWindow
    {
        IntPtr GetWindow();
        void ContextSensitiveHelp(bool enterMode);
    }
    
    
    public class Win32WindowFromOleWindow : IWin32Window, IDisposable
    {
#region Members
    
        private COMIOleWindow iolewindow;
    
#endregion Members
    
#region Constructors
    
        public Win32WindowFromOleWindow(object window) : this(window as COMIOleWindow)
        {
        }
    
        public Win32WindowFromOleWindow(COMIOleWindow iolewindow)
        {
            this.iolewindow = iolewindow;
            if (this.iolewindow == null)
                throw new ArgumentNullException();
        }
    
#endregion Constructors
    
#region IDisposable
    
        public void Dispose()
        {
            this.iolewindow = null;
        }
    
        private void CheckDisposed()
        {
            if (this.iolewindow == null)
                throw new ObjectDisposedException("Win32WindowFromOleWindow");
        }
    
#endregion IDisposable

#region IWin32Window properties
    
        public IntPtr Handle
        {
            get
            {
                this.CheckDisposed();
    
                return this.iolewindow.GetWindow();
            }
        }
    
#endregion IWin32Window properties
    }


    public class HwndWrapper : COMIOleWindow, IWin32Window
    {
#region Members

        private IntPtr hwnd;

#endregion Members

#region Constructors

        public HwndWrapper(IntPtr hwnd)
        {
            this.hwnd = hwnd;
            if (this.hwnd == IntPtr.Zero)
                throw new ArgumentNullException();
        }

#endregion Constructors

#region COMIOleWindow methods

        public IntPtr GetWindow()
        {
            return this.Handle;
        }

        public void ContextSensitiveHelp(bool enterMode)
        {
            // NYI
        }

#endregion COMIOleWindow methods

#region IWin32Window properties
    
        public IntPtr Handle
        {
            get
            {
                return this.hwnd;
            }
        }
    
#endregion IWin32Window properties
    }


    //public class BitMapHelper
    //{

    //    public Bitmap CreateBitmap()
    //    {
    //        int sideSize = this.panel1.Width;
    //        string filePath = @"C:\Projects\SAFESignature\safelogo.png";

    //        Image safeImage = new Bitmap(filePath);

    //        Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
    //        Graphics g = Graphics.FromImage(bmp);


    //        RectangleF df = new RectangleF(0, 0, panel1.Width, panel1.Height);
    //        RectangleF sf = new RectangleF(0, 0, safeImage.Width, safeImage.Height);
    //        g.DrawImage(safeImage,
    //            df,
    //            sf,
    //            GraphicsUnit.Pixel);



    //        string drawString = "This is a test";
    //        Font drawFont = new Font("Arial", 16);
    //        SolidBrush drawBrush = new SolidBrush(Color.Black);


    //        // Draw rectangle to screen.
    //        Pen blackPen = new Pen(Color.Black);
    //        //e.Graphics.DrawRectangle(blackPen, x, y, width, height);

    //        // Set format of string.
    //        StringFormat drawFormat = new StringFormat();
    //        drawFormat.Alignment = StringAlignment.Center;

    //        // Draw string to screen.
    //        g.DrawString(drawString, drawFont, drawBrush, df, drawFormat);

    //        g.Dispose();

    //        return bmp;
    //    }

    //}
} 
